﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace IceCream.icemodel
{
    public class ElseSerialPort
    {
        private static ElseSerialPort instance;
        private ElseSerialPort() { }
        public static ElseSerialPort Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ElseSerialPort();
                }
                return instance;
            }
        }
        /// <summary>
        /// 串口实例
        /// </summary>
        public SerialPort serialPort = new SerialPort();
        /// <summary>
        /// 接收数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        /// <summary>
        /// 查询检测类型
        /// </summary>
        public List<string> DigitalMsgList = new List<string>();

        //是否发送文本
        bool sendText = false;
        /// <summary>
        /// 串口连接
        /// </summary>
        /// <param name="PortName">通讯端口</param>
        /// <param name="BaudRate">波特率</param>
        /// <param name="Parity_Sel">奇偶校验</param>
        /// <param name="DataBits">每个字节标准数据位长度</param>
        /// <param name="StopBits">停止位</param>
        public void IceMachineConnect(string PortName, int BaudRate, string Parity_Sel, int DataBits, double StopBits)
        {
            try
            {
                if (!serialPort.IsOpen)
                {
                    //设定参数PortName
                    serialPort.PortName = PortName;
                    serialPort.BaudRate = BaudRate;
                    serialPort.Parity = GetSelectedParity(Parity_Sel);
                    serialPort.DataBits = DataBits;
                    serialPort.StopBits = GetSelectedStopBits(StopBits);
                    serialPort.Encoding = Encoding.UTF8;
                    //InitIceMachine();
                    try
                    {
                        serialPort.Close();
                        //打开串口
                        serialPort.Open();
                        // DigitalMsgList.Add("ok");
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
                else
                {
                    if (serialPort != null)
                    {
                        //关闭串口
                        serialPort.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
        /// <summary>
        /// 其他的发送指令
        /// </summary>
        /// <param name="str"></param>
        public void SerialSend(string str)
        {
            try
            {
                str = str.Replace(" ", "").Replace("\r\n", "");
                //将输入的16进制字符串两两分割为字符串集合
                var strArr = Regex.Matches(str, ".{2}").Cast<Match>().Select(m => m.Value);
                //需要发送的字节数组
                byte[] data = new byte[strArr.Count()];
                //循环索引
                int temp = 0;
                //将字符串集合转换为字节数组
                foreach (string item in strArr)
                {
                    data[temp] = Convert.ToByte(item, 16);
                    temp++;
                }
                //发送字节
                serialPort.Write(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                serialPort.Close();
                DigitalMsgList.Clear();
                serialPort.Open();
                return;
            }
        }
        /// <summary>
        /// 获取窗体选中的停止位
        /// </summary>
        /// <returns></returns>
        private StopBits GetSelectedStopBits(double StopBits_Sel)
        {
            switch (StopBits_Sel)
            {
                case 1:
                    return StopBits.One;
                case 1.5:
                    return StopBits.OnePointFive;
                case 2:
                    return StopBits.Two;
                default:
                    return StopBits.One;
            }
        }
        /// <summary>
        /// 获取窗体选中的奇偶校验
        /// </summary>
        /// <returns></returns>
        public Parity GetSelectedParity(string Paritysel)
        {
            switch (Paritysel)
            {
                case "Odd":
                    return Parity.Odd;
                case "Even":
                    return Parity.Even;
                case "Mark":
                    return Parity.Mark;
                case "Space":
                    return Parity.Space;
                case "None":
                default:
                    return Parity.None;
            }
        }
    }
}
